package com.example.demo.Gmail.Excutor;

import com.example.demo.Gmail.Handle.GmailHandle;
import com.example.demo.Gmail.Handle.proxyHandler;
import com.example.demo.Gmail.model.UserModel;
import com.example.demo.Gmail.service.UserService;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.model.History;
import com.google.api.services.gmail.model.HistoryMessageAdded;
import com.google.api.services.gmail.model.ListHistoryResponse;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;

/**
 * 处理监控的消息，导入数据库和发送消息给前端
 */
public class AscWatchMailChange  {
    private UserModel user;
    private static Logger logger = LoggerFactory.getLogger("AscWatchMailChange");
    public AscWatchMailChange( UserModel user){
        this.user=user;
    }

    public Map call()  {
        Map<String, Set<String>> map= Maps.newHashMap();
        //大陆环境需要加代理
        proxyHandler.proxy();
        try {
            Gmail service= GmailHandle.service(user);
            List<History> histories = new ArrayList<History>();
            ListHistoryResponse historyresponse = service.users().history().list("me")
                    .setStartHistoryId(BigInteger.valueOf(Long.valueOf(user.getHistoryId()))).execute();
            while (historyresponse.getHistory() != null) {
                histories.addAll(historyresponse.getHistory());
                if (historyresponse.getNextPageToken() != null) {
                    String pageToken = historyresponse.getNextPageToken();
                    historyresponse = service.users().history().list("me").setPageToken(pageToken)
                            .setStartHistoryId(BigInteger.valueOf(Long.valueOf(user.getHistoryId()))).execute();
                } else {
                    break;
                }
            }
            Set sentLabel= Sets.newHashSet();
            Set inboxLabel= Sets.newHashSet();
            for (History history : histories) {
                if (history.getMessagesAdded()!=null){
                    for (HistoryMessageAdded historyMessageAdded:history.getMessagesAdded()){
                        for (String labelId:historyMessageAdded.getMessage().getLabelIds()){
                            if (labelId.equals("SENT")){
                                sentLabel.add(historyMessageAdded.getMessage().getId());
                                map.put(labelId,sentLabel);
                            }else if (labelId.equals("INBOX")){
                                inboxLabel.add(historyMessageAdded.getMessage().getId());
                                map.put(labelId,inboxLabel);
                            }
                        }
                    }
                }
            }

        }catch (Exception e){
            logger.info(e.getMessage());
        }
        return map;
    }
}
